/*
 * @Author: xgq
 * @Date: 2020-05-14 16:47:04
 * @LastEditTime: 2020-06-09 15:06:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\models\m_user_downloads.js
 */

const Sequelize = require('sequelize')



module.exports = (sequelize, DataTypes) => {

    class userDownloads extends Sequelize.Model {

        // 根据用户id查出下载过的图片
        static async getImageIdByUidInOrder(pars) {
            // let { user_id, page, pagesize } = pars
            var [page=1,pagesize=15,user_id, order = null, offset = null, limit = null] = [pars.page,pars.pagesize,pars.user_id, pars.order, pars.offset, pars.limit]
            let sql = "select images.* from images left join user_downloads ud on images.id = ud.img_id where ud.down_type = :down_type and ud.user_id = :user_id and images.is_enable=0  order by ud.create_time DESC limit :offset,:pagesize"
            let imageList = await sequelize.query(
                sql, //原生 SQL 语句，参数用 ? 
                { replacements: {down_type:0, user_id: user_id, offset: offset, pagesize: parseInt(pagesize) }, type: Sequelize.QueryTypes.SELECT } //replacements替换SQL中的参数
            );
            let countSql = "select count(*) as count from images left join user_downloads ud on images.id = ud.img_id where ud.down_type = :down_type and ud.user_id = :user_id and images.is_enable=0 "
            let count = await sequelize.query(
                countSql, //原生 SQL 语句，参数用 ? 
                { replacements: {down_type:0, user_id: user_id }, type: Sequelize.QueryTypes.SELECT } //replacements替换SQL中的参数
            );
            var result = {
                rows: imageList,
                count: count[0].count
            }
            return result;
        }

        //获取专题 
        static async gettopicInner(id) {
            var querydata = []
            let condition = {
                attributes: ['id', 'topic_id', 'associated_id', 'tp_type'],
                raw: true,
                where: {
                    topic_id: id
                }
            }
            querydata = await this.findAll(condition)
            return querydata;
        }
        // 根据用户id查出下载过的图片
        static async getImageIdByUid(uid) {
            var condition = {
                attributes: ['img_id'],
                where: {
                    user_id: uid,
                    down_type: 0
                },
                raw: true
            }
            var list = await this.findAll(condition)
            return list;
        }
       
    }

    userDownloads.init({
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        img_id: {
            type: DataTypes.INTEGER
        },
        user_id: {
            type: DataTypes.INTEGER
        },
        down_type: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        down_size: {
            type: DataTypes.STRING(45)
        },
        down_format: {
            type: DataTypes.STRING(45)
        },
        create_time: {
            type: DataTypes.INTEGER
        }
    }, {
        sequelize,
        modelName: 'user_downloads',
        freezeTableName: true,
        timestamps: false
    });

    return userDownloads;

}